<template>
  <div class="search-bar-container">
    <div class="search-info">
      <div class="search-bar">
        <div class="left">
          <div class="dropdown">
            <el-dropdown>
              <span class="el-dropdown-link">
                <span>{{ selectQuery }}</span>
                <el-icon class="el-icon--right" style="font-size: 24px">
                  <arrow-down />
                </el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="handelSelect('全部')">全部</el-dropdown-item>
                  <el-dropdown-item @click="handelSelect('GMAT')">GMAT</el-dropdown-item>
                  <el-dropdown-item @click="handelSelect('GRE')">GRE</el-dropdown-item>
                  <el-dropdown-item @click="handelSelect('EA')">EA</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <el-divider direction="vertical" />
          </div>

          <input class="search-input" v-model="searchQuery" placeholder="Search..." />
        </div>
        <div class="search" @click="handelSearch">
          <img src="~@/static/search-icon.png" alt="" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { Plus, CirclePlusFilled } from "@element-plus/icons-vue"; // 引入图标
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from "@/stores/User";
const userStore = useUserStore();
const router = useRouter()
const route = useRoute()

const selectQuery = ref("全部");
const searchQuery = ref(userStore.searchTitle);

const handelSelect = (value) => {
  selectQuery.value = value;
};

const handelSearch = () => {
  console.log(`Searching for: ${searchQuery.value}`);
  let type;
  userStore.searchTitle = searchQuery.value;
  if (selectQuery.value == "全部") type = 0;
  else if (selectQuery.value == "GMAT") type = 1;
  else if (selectQuery.value == "GRE") type = 2;
  else if (selectQuery.value == "EA") type = 3;
  router.push(`/questionList?type=${type}`);
};

watch(
  () => userStore.searchTitle,
  () => searchQuery.value = userStore.searchTitle
);
</script>

<style scoped>
.search-bar-container {
  display: flex;
  width: 100%;
  justify-content: center;
  padding-bottom: 10px;
}

.search-info {
  width: 778px;
  height: 48px;
  background-color: #ffffff;
  box-shadow: 0px 0px 4px #000000;
  border-radius: 40px;
  padding: 0px 30px;
  display: flex;
  align-items: center;
}
.left {
  flex: 1;
  /* width:; */
}
.search {
  width: 24px;
  height: 24px;
  cursor: pointer;
  img {
    width: 100%;
    height: 100%;
  }
}

.search-input {
  flex-grow: 1;
  padding: 10px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  /* overflow:; */
  outline: none;
}

.search-button {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  background-color: #007bff;
  color: #fff;
  font-size: 16px;
  cursor: pointer;

  &:hover {
    background-color: #0056b3;
  }
}
.dropdown {
  display: flex;
  align-items: center;
}
.el-dropdown-link {
  display: flex;
  align-items: center;
}
.search-bar {
  width: 100%;
  display: flex;
  align-items: center;
  .left {
    display: flex;
    align-items: center;
  }
}
</style>
